#!/bin/bash
set -e
set -o pipefail

# 获取脚本路径
current_dir=$(cd $(dirname $0); pwd)
# 导入公共方法
source ${current_dir}/common.sh
# 导入配置文件
source ${current_dir}/config.cnf


# 安装前检查
function before_install_check(){
    comm_print_note "检测数据文件是否存在且为空"
    comm_is_dir_empty ${pg_dir}/data
    comm_print_note "检测PG端口是否已存在"
    comm_check_port_occupied $pg_port
}

# 系统相关设置
function system_set(){
    comm_print_note "关闭selinux"
    comm_system_disabe_selinux
    comm_print_note "关闭防火墙"
    comm_do_stop_firewalld
    comm_print_note "优化资源配置"
    comm_init_system_limits
    comm_print_note "优化内核参数"
    comm_optimize_system_kernel_parameters
    comm_print_note "关闭透明大页"
    comm_disable_transparent_hugepage
    comm_print_note "配置时间同步"
    comm_ntpd
}

# 检测是否能连接外网
function check_connect_internet(){
    local ret_code=$(curl -I -s --connect-timeout 1 www.baidu.com -w %{http_code}|tail -n1)
    if [ "x$ret_code" = "x200" ];then
        return 0
    else
        comm_print_error "执行程序 $0 ,检测连接百度失败，判断无法连接外网,程序退出..."
        return 1
    fi
}

# 挂载本地yum/apt源
function mount_local(){
  if [ ! -d /tmp/pglocal_local ];then
    tar zxf ${current_dir}/pglocal_local.tar.gz
    mv ${current_dir}/pglocal_local /tmp/

    cat > /etc/yum.repos.d/pglocal_local.repo <<EOF
[local]
name=pglocal_local
baseurl=file:///tmp/pglocal_local
gpgcheck=0
enabled=1
EOF
  else 
    comm_print_note "不存在离线安装包，将使用在线下载方式安装.."
  fi
}


# 部署单机模式
function deploy_pg_single(){
    comm_print_note "正在进行安装前预检查..."
    before_install_check
    comm_print_note "设置系统相关..."
    system_set
    comm_print_note "部署单机版PG..."
    ./deploy_pg.sh single
    comm_print_note "部署备份归档工具"
    ./deploy_backup_pgbackrest.sh deploy
    comm_print_note "部署单机版PG完成"
}

# 部署patroni高可用模式
function deploy_ha_patroni(){
    comm_print_note "正在进行安装前预检查..."
    before_install_check
    comm_print_note "设置系统相关..."
    system_set
    comm_print_note "安装PG..."
    ${current_dir}/deploy_pg.sh pgonly
    comm_print_note "部署备份归档工具..."
    ${current_dir}/deploy_backup_pgbackrest.sh install
    comm_print_note "部署patroni..."
    ${current_dir}/deploy_patroni.sh deploy_patroni
    #comm_print_note "配置 pgbackrest，创建存储空间、检查、配置定时任务"
    ${current_dir}/deploy_backup_pgbackrest.sh create_check
    comm_print_note "节点部署完成"
}

# 使用说明
usage () {
        cat <<EOF
Usage: $0 [OPTIONS]
  single                 安装单机版
  etcd_cluster           安装集群版 etcd                                         
  patroni_cluster        安装patroni集群版本
  uninstall_all          卸载所有环境
EOF
exit
}

# main 入口
command="${1}"
case "${command}" in
    "single" )
        deploy_pg_single
    ;;
    "etcd_cluster" )
        ${current_dir}/deploy_etcd.sh cluster
    ;;
    "patroni_cluster" )
        deploy_ha_patroni
    ;;
    "uninstall_etcd" )
	    ${current_dir}/deploy_etcd.sh uninstall
    ;;
    "uninstall_pg" )
	    ${current_dir}/deploy_pg.sh uninstall
    ;;
    "uninstall_all" )
        ${current_dir}/deploy_etcd.sh uninstall
	    ${current_dir}/deploy_pg.sh uninstall
        ${current_dir}/deploy_backup_pgbackrest.sh uninstall
        ${current_dir}/deploy_patroni.sh uninstall
    ;;
    * )
        usage
    ;;
esac
